/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ueb17;

/**
 *
 * @author steffenschuhmann
 */
public class HashTableImpl {
    
    private static int STDSIZE = 10000;
    
    private HashNode[] table;
    
    public HashTableImpl() {
        table = new HashNode[STDSIZE];
    }
    
    private int hash(String name) {
        int hashValue;
        
        if(name == null)
            throw new NullPointerException();
        
        hashValue = name.hashCode() % 1000;
        return hashValue;
    }
    
    public void insert(Nameable data) throws WrongTypeException, NotNameblaException{
        HashNode newNode;
        int hashValue;
       
        newNode = new HashNode(data);
        
        hashValue = hash(data.getName());
        if(table[hashValue] == null) {
            table[hashValue] = newNode;
        } else {
            newNode.setNext(table[hashValue]);
            table[hashValue] = newNode;
        }
            
    }
    
    public Object find(String name) {
        int hashValue;
        HashNode actualNode;
        
        hashValue = hash(name);
        
        if(table[hashValue] != null) {
            actualNode = table[hashValue]; 
            if(actualNode.getName().equals(name))
                return actualNode;
            while(actualNode.hasNext()) {
                actualNode = actualNode.getNext();
                if(actualNode.getName().equals(name))
                    return actualNode.getData();
            }
        }       
        return null;
    } 
    
    public String toStringHelper(HashNode node, StringBuilder builder) {
        if(node.hasNext())
            toStringHelper(node.getNext(), builder);
        builder.append(node.getName());
        builder.append("<-->");
        
        return builder.toString();
    }
    
    @Override
    public String toString() {
        StringBuffer tmpString = new StringBuffer();
        
        for(int n = 0; n < table.length; n++) {
            if(table[n] != null)
                tmp
        }
        
        return tmpString.toString();
    }
    
}
